"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LoopAnimationPlayer = exports.AnimationPlayer = void 0;
/*
 * @Author: hongbin
 * @Date: 2023-01-01 19:37:53
 * @LastEditors: hongbin
 * @LastEditTime: 2023-03-01 22:08:17
 * @Description: 动画控制器
 */
var three_1 = require("three");
/**
 * 每个带动画的模型创建一个对应的播放器 用来控制该模型的动画进度
 */
var AnimationPlayer = /** @class */ (function () {
    function AnimationPlayer(params) {
        this.timer = 0;
        this.clock = new three_1.Clock();
        this.animationActions = [];
        this.maxDuration = 0;
        /**
         * 每个动画的每个变化属性的第一帧数据
         */
        this.firstKeyframes = {};
        this.lastKeyframes = {};
        params && this.init(params.root, params.animations);
    }
    /**
     * 初始化动画 计算所需数据
     */
    AnimationPlayer.prototype.init = function (root, animations) {
        var _this = this;
        var mixer = new three_1.AnimationMixer(root);
        this.mixer = mixer;
        // console.log(animations);
        animations.forEach(function (animate) {
            // const animationAction = mixer.clipAction(animate).play();
            var animationAction = mixer
                .clipAction(animate)
                .play()
                .setLoop(three_1.LoopOnce, 1);
            //获取时间 _clip.duration 也可 不过three没报漏出来那就不用"_"开头的变量一般内部使用
            if (animate.duration > _this.maxDuration)
                _this.maxDuration = animate.duration;
            // console.log("绑定动画", animationAction);
            _this.animationActions.push(animationAction);
            // this.handleKeyframe(animate);
        });
        // console.log(this.firstKeyframes);
        // console.log(this.lastKeyframes);
    };
    /**
     * 计算动画第一帧数据和最后一帧数据
     */
    AnimationPlayer.prototype.handleKeyframe = function (animate) {
        var firstKeyframe = {};
        var lastKeyframe = {};
        animate.tracks.forEach(function (track) {
            if (!["quaternion", "vector"].includes(track.ValueTypeName))
                throw new Error("未处理类型" + track.ValueTypeName);
            firstKeyframe[track.ValueTypeName] = {
                x: track.values[0],
                y: track.values[1],
                z: track.values[2],
            };
            var length = track.values.length;
            lastKeyframe[track.ValueTypeName] = {
                x: track.values[length - 3],
                y: track.values[length - 2],
                z: track.values[length - 1],
            };
            // 旋转使用 四元数表示
            if (track.ValueTypeName == "quaternion") {
                firstKeyframe[track.ValueTypeName].w = track.values[3];
                lastKeyframe[track.ValueTypeName].w = track.values[length - 4];
            }
        });
        this.firstKeyframes[animate.name] = firstKeyframe;
        this.lastKeyframes[animate.name] = lastKeyframe;
    };
    /**
     * 开始播放,模型动画传入进度百分比
     */
    AnimationPlayer.prototype.play = function (percent) {
        // if (!this.mixer) throw new Error("未初始化mixer");
        if (!this.mixer)
            return;
        percent > 0.99 && (percent = 0.99);
        var t = this.maxDuration * percent;
        /**
         * 模型中多个物体每个物体动画时常不同 以下处理超出时常的不播放
         * 待确认是否 setLoop(LoopOnce, 1) 作用
         * 确认后删除多余的注释部分
         */
        this.animationActions.forEach(function (action) {
            action.stop();
            action.play();
        });
        this.mixer.setTime(t);
    };
    AnimationPlayer.prototype.update = function () {
        if (!this.mixer)
            return;
        var delta = this.clock.getDelta();
        this.mixer.update(delta);
    };
    return AnimationPlayer;
}());
exports.AnimationPlayer = AnimationPlayer;
/**
 * 循环播放的动画
 */
var LoopAnimationPlayer = /** @class */ (function () {
    function LoopAnimationPlayer(params) {
        this.timer = 0;
        this.clock = new three_1.Clock();
        this.animationActions = [];
        params && this.init(params.root, params.animations);
    }
    /**
     * 初始化动画 计算所需数据
     */
    LoopAnimationPlayer.prototype.init = function (root, animations) {
        var _this = this;
        var mixer = new three_1.AnimationMixer(root);
        this.mixer = mixer;
        animations.forEach(function (animate) {
            var animationAction = mixer.clipAction(animate).play();
            _this.animationActions.push(animationAction);
        });
    };
    LoopAnimationPlayer.prototype.update = function () {
        if (!this.mixer)
            return;
        var delta = this.clock.getDelta();
        this.mixer.update(delta);
    };
    return LoopAnimationPlayer;
}());
exports.LoopAnimationPlayer = LoopAnimationPlayer;
